var express = require('express');
var router = express.Router();

const SchedulingModel = require('../db/SchedulingModel');
const Fun = require("../middlewares/Fun")
const moment = require('moment');
const now = () => {
    return moment().format('YYYY-MM-DD HH:mm:ss')
}
/***
 *  list 班次信息列表
 *  one 查找指定班次信息
 *  save 添加班次信息
 *  update 更新班次信息
 *  delete 删除班次信息
 */
router.post('/hadnamed', function (req, res, next) {
    if (!req.body.name) {
        return res.json({ code: 201, mes: '缺少必填信息！' });
    }

    SchedulingModel.findOne({ name: req.body.name }).then((data) => {
        if (data) {
            return res.json({ code: 201, mes: '该名称已经存在！' });
        } else {
            return res.json({ code: 200, mes: '该名称可用！' });
        }
    }).catch(() => {
        return res.json({ code: 201, mes: '发生意外错误！' })
    });

});
// 创建班次
router.post('/save', function (req, res, next) {
    // 获取条件
    if (!req.body.name || !req.body.state || !req.body.workingtime) {
        return res.json({ code: 201, mes: '缺少必填信息！' });
    }
    let user = req.user
    const scheduling = new SchedulingModel(req.body);
    scheduling.workingtime = JSON.parse(req.body.workingtime);
    SchedulingModel.find({ name: req.body.name }).then((data) => {
        if (data.length != 0) { return res.json({ code: 201, mes: '班次名称已经存在' }); }
        else {
            scheduling.save().then(() => {
                Fun.saveLogs(user, req.ip.replace(/::ffff:/, ''), "班次创建成功");
                return res.json({ code: 200, mes: '班次创建成功' })
            }).catch((err) => {
                console.log(err);
                Fun.saveLogs(user, req.ip.replace(/::ffff:/, ''), "班次创建失败");
                return res.json({ code: 201, mes: '发生意外错误！' })
            });
        }
    }).catch((err) => {
        console.log(err);
        return res.json({ code: 201, mes: '发生意外错误！' });
    });
});

// 更新来源信息
router.post('/update', function (req, res, next) {
    // 更改密码的判断需要额外处理
    const set = req.body;
    if (req.body.workingtime) {
        set.workingtime = JSON.parse(req.body.workingtime);
    }
    let user = req.user
    const id = req.body.id;
    SchedulingModel.findById(id).then(
        (data) => {
            if (data == null) { return res.json({ code: 201, mes: '传入id错误' }); }
            else {
                return SchedulingModel.update({ _id: id }, { $set: set }).then(
                    () => { Fun.saveLogs(user, req.ip.replace(/::ffff:/, ''), "班次修改成功"); return res.json({ code: 200, mes: '更改成功' }); }
                ).catch(() => {
                    Fun.saveLogs(user, req.ip.replace(/::ffff:/, ''), "班次修改失败");
                    return res.json({ code: 201, mes: '更改失败！' })
                })
            }
        }
    ).catch(function () {
        return res.json({ code: 201, mes: '发生意外错误！' });
    });
});

// 查找所有来源
router.get('/list', function (req, res, next) {
    SchedulingModel.find({}).then((schedulings) => {
        return res.json({ code: 200, schedulings });
    }).catch(() => {
        return res.json({ code: 201, mes: '发生意外错误！' });
    });
});

// 查找指定来源
router.get('/one/:id', function (req, res, next) {
    const id = req.params.id;
    SchedulingModel.findById(id).then((source) => {
        return res.json({ code: 200, source });
    }).catch(() => {
        return res.json({ code: 201, mes: '发生意外错误！' });
    });
});

// 删除接口来源
router.post('/delete', function (req, res, next) {
    let user = req.user
    const id = req.body.id;
    SchedulingModel.remove({ _id: id }).then(() => {
        Fun.saveLogs(user, req.ip.replace(/::ffff:/, ''), "班次删除成功");
        return res.json({ code: 200, mes: '删除成功！' });
    }).catch(() => {
        Fun.saveLogs(user, req.ip.replace(/::ffff:/, ''), "班次删除失败");
        return res.json({ code: 201, mes: '发生意外错误！' });
    });
});
module.exports = router;
